package com.venue.hjt.venueclass;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.venue.hjt.venueclass.db.SchArticle;

import java.util.List;

/**
 * Created by xqx on 2018/1/9.
 *
 */


public class MemberFingerprintAdapter extends RecyclerView.Adapter<MemberFingerprintAdapter.ViewHolder>{
    private LayoutInflater mInflater;
    private List<SchArticle> memberFingerDataModel;
    private OnItemClickLitener mOnItemClickLitener;
    private int mCurrentPos;
    /**
     * ItemClick的回调接口
     * @author zhy
     */
    public interface OnItemClickLitener{
        void onItemClick(View view, int position);
    }


    public void setCurrentPosition(int i){
        this.mCurrentPos = i;
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener){
        this.mOnItemClickLitener = mOnItemClickLitener;
    } public MemberFingerprintAdapter(Context context, List<SchArticle> memberFingerDataModel) {
        mInflater = LayoutInflater.from(context);
        this.memberFingerDataModel = memberFingerDataModel;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }
        LinearLayout llDelFingerprint;
        CardView ll_dongBg;
        WebView web;
        ImageView iv_dongImg;
        TextView tv_dongTitle,tv_dongContent;
    }

    @Override
    public int getItemCount() {
        return memberFingerDataModel.size();
    }

/**
 * 创建ViewHolder
 */
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rv_fingerprint,viewGroup,false);
    final ViewHolder viewHolder = new ViewHolder(view);
//    viewHolder.tvFingerName =  view.findViewById(R.id.tvFingerName);
    viewHolder.llDelFingerprint = view.findViewById(R.id.llDelFingerprint);
    viewHolder.ll_dongBg = view.findViewById(R.id.ll_dongBg);
    viewHolder.web=view.findViewById(R.id.webView1);
    viewHolder.iv_dongImg=view.findViewById(R.id.iv_dongImg);
    viewHolder.tv_dongTitle=view.findViewById(R.id.tv_dongTitle);
    viewHolder.tv_dongContent=view.findViewById(R.id.tv_dongContent);
    viewHolder.web.getSettings().setUseWideViewPort(true);
    viewHolder.web.getSettings().setJavaScriptEnabled(true);
    //不使用缓存：
    viewHolder.web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    viewHolder.web.getSettings().setSupportZoom(true); //设置可以支持缩放
    viewHolder.web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
    // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
    viewHolder.web.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            //设置加载进度条
            return true;
        }

        @Override
        public void onPageFinished(final WebView view, String url) {
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)viewHolder.web.getLayoutParams();
//                    lp.height=viewHolder.web.getContentHeight();
//                    Log.e("ATG",viewHolder.web.getContentHeight()+"  内容高度");
//                    viewHolder.web.setLayoutParams(lp);
//                }
//            },2000);

            super.onPageFinished(view, url);
        }
//        doUpdateVisitedHistory
    });

    return viewHolder;
}

/**
 * 设置值
 */
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
//    viewHolder.setIsRecyclable(false);
    SchArticle schArticle = this.memberFingerDataModel.get(i);
    if (schArticle.getIsClose()){
        Log.e("ATG","lai"+mCurrentPos);
        viewHolder.web.loadUrl(schArticle.getLink());
        viewHolder.llDelFingerprint.setVisibility(View.VISIBLE);
    }else{
        Log.e("ATG","lai");
        viewHolder.llDelFingerprint.setVisibility(View.GONE);
    }
    Glide.with(mInflater.getContext()).load(schArticle.getImage_path()).into(viewHolder.iv_dongImg);
    viewHolder.tv_dongTitle.setText(schArticle.getTitle());
    viewHolder.tv_dongContent.setText(schArticle.getContent().replace(" ",""));
    //如果设置了回调，则设置点击事件
    if (mOnItemClickLitener != null) {
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
            }
        });
    }
}
}
